//: Playground - noun: a place where people can play

import UIKit

var str = "Hello, playground"

// 区间运算符
for index in 1...5 {
    print("index:\(index)")
}


for index in 1..<5 {
    print("index=:\(index)")
}


// Switch
print("============")
let index = 15
switch index {
case 100:
    print("index is 100")
case 10, 15:
    print("index is 10 to 15")
case 5:
    print("index is 5")
default:
    print("default")
}

// While 循环
var index2 = 10
while index2 < 20 {
    print("index value is: \(index2)")
    index2 += 1
}

repeat {
    print("index value is==: \(index2)")
    index2 += 1
} while (index2 < 30)


// 字符串处理
var urlString = "http://www.baidu.com";
if urlString.isEmpty {
    print("urlString is empty")
} else {
    print("urlString is Not Empty")
}

if urlString.hasPrefix("http") {
    print("urlString has Prefix Http")
}

if urlString.hasSuffix("com") {
    print("urlString has Suffix com")
}

print("String.characters.count = \(urlString.characters.count)")
print("String length = \(urlString.count)")
String.init(format: "hello %@", "aaaa")

// 字符处理
//error: Operatior.playground:59:23: error: cannot convert value of type 'String' to specified type 'Character'
//let char: Character = "AB" //cannot convert value of type 'String' to specified type 'Character'
//let char: Character = "AB"

for ch in "Hello".characters {
    print(ch)
}


for ch in "World" {
    print(ch)
}

// 数组处理
var someInts:[Int] = [10, 20, 30]
var someVar = someInts[0]
print("someVar=\(someVar)")

someInts.append(69)
print(someInts)


var intsA = repeatElement(3, count: 3)
print("\(intsA.count)  \(intsA)")

for intValue in intsA {
    print(intValue)
}

// 字典处理
var someDict = [Int: String]()
someDict.startIndex

someDict = [1: "One", 2:"Two"]
print(someDict)
print(someDict[1])
print(someDict[3])
someDict.updateValue("Four", forKey: 4)
someDict.updateValue("Three", forKey: 3)

for (key, vallue) in someDict {
    print("\(key) => \(vallue)")
}

var keys = someDict.keys
for key in keys {
    print(key)
}

for value in someDict.values {
    print(value)
}

// 方法处理
func add(valueA: Int, valueB: Int) -> Int {
    return valueA + valueB;
}
var addResult = add(valueA: 10, valueB: 20)
print(addResult)

// 函数返回元组，？表示可选元组型，表示的是整个元组是可选的
func minMax(array: [Int]) -> (min: Int, max: Int)? {
    if array.count <= 0 {
        return nil
    }
    var currentMin = array[0]
    var currentMax = currentMin;
    for index in 1..<array.count {
        if currentMin > array[index] {
            currentMin = array[index]
        } else if currentMax < array[index]{
            currentMax = array[index]
        }
    }
    return (currentMin, currentMax)
}

var minMaxValue = minMax(array: [1, 2, 3, 4, 5])
if let minMax = minMaxValue {
    print(minMax)
}

// 函数的外部参数和内部参数
func power(firstArg a: Int, secondArg b:Int) -> Int {
    var result = a
    for _ in 1..<b {
        result *= a
    }
    return result;
}

print(power(firstArg: 3, secondArg: 4))

// 函数的I/O参数
func swapValue(_ a: inout Int, _ b: inout Int) {
    a += b;
    b = a-b;
    a = a-b;
}

var a = 10, b = 20
swapValue(&a, &b)
print("a = \(a)  b=\(b)")


// 闭包
let simpleBlock = { print("世界上最简单的闭包")}
simpleBlock()

// (Int, Int) -> Int 类型的闭包
let sumBlock = {(vala: Int, valb:Int) -> Int in
    return vala + valb
}
print(sumBlock(10, 20))

let names = ["a", "b", "d", "c"]
// s1 > s2 表示前面的值大于后面的值，结果倒序
// s1 < s2 表示前面的值小于后面的值，结果正序
var sortedNames = names.sorted { (s1: String, s2: String) -> Bool in
    return s1 > s2
}
print(sortedNames)

// 正序 简写1
sortedNames = names.sorted(by: {$0 < $1})
print(sortedNames)

// 正序 简写2
sortedNames = names.sorted(by: <)
print(sortedNames)

// 正序 尾随闭包写法1，方法最后一个参数是闭包
sortedNames = names.sorted(){$0 < $1}
print(sortedNames)

// 正序 尾随闭包写法1
sortedNames = names.sorted{$0 < $1}
print(sortedNames)

func makeIncrementor(forIncrement amount: Int) -> ()->Int {
    var runninTotal = 0
    func incrementor() -> Int {
        runninTotal += amount
        return runninTotal
    }
    return incrementor
}
var incTenFunc = makeIncrementor(forIncrement: 10)
print(incTenFunc())
print(incTenFunc())
print(incTenFunc())

